-- ********************************************
-- * Copyright (c) 2012 TPV R&D Center Brazil *
-- * All Rights Reserved                      *
-- ********************************************

--- toolkit.component.Label

local Label = Box:new()

function Label:new(o)
   if (type(o) ~= 'table') then
      o = {}
   end

   local newLabel = Box:new(o)

   newLabel.parent = nil

   newLabel.componentType = "Label"

   newLabel.text = o.text or ''
   newLabel.behavior = o.behavior or nil

   newLabel.activated = {
      size = 20,
      color = {241, 241, 241, 255},
      type = 'Lato',
      style = 'Normal'
   }

   newLabel.deactivated = {
      size = 20,
      color = {241, 241, 241, 255},
      type = 'Lato',
      style = 'Normal'
   }

   newLabel.focused = {
      size = 20,
      color = {241, 241, 241, 255},
      type = 'Lato',
      style = 'Normal'
   }

   newLabel.selected = {
      size = 20,
      color = {52, 176, 255, 255},
      type = 'Lato',
      style = 'Normal'
   }

   newLabel.shadow = {
      shift = 0,
      color = {0, 0, 0, 255}
   }

   newLabel.align = o.align or 'LEFT'
   newLabel.spacing = o.spacing or 20

   --generate an object
   setmetatable(newLabel, self)
   self.__index = self

   return newLabel
end

function Label:setActivated(o)
   if (type(o) == 'table') then
      self.activated.size = o.size or self.activated.size
      self.activated.color = o.color or self.activated.color
      self.activated.type = o.type or self.activated.type
      self.activated.style = o.style or self.activated.style
   end
end

function Label:setDeactivated(o)
   if (type(o) == 'table') then
      self.deactivated.size = o.size or self.deactivated.size
      self.deactivated.color = o.color or self.deactivated.color
      self.deactivated.type = o.type or self.deactivated.type
      self.deactivated.style = o.style or self.deactivated.style
   end
end

function Label:setFocused(o)
   if (type(o) == 'table') then
      self.focused.size = o.size or self.focused.size
      self.focused.color = o.color or self.focused.color
      self.focused.type = o.type or self.focused.type
      self.focused.style = o.style or self.focused.style
   end
end

function Label:setSelected(o)
   if (type(o) == 'table') then
      self.selected.size = o.size or self.selected.size
      self.selected.color = o.color or self.selected.color
      self.selected.type = o.type or self.selected.type
      self.selected.style = o.style or self.selected.style
   end
end

function Label:setShadow(o)
   if (type(o) == 'table') then
      self.shadow.shift = o.shift or 1
      self.shadow.color = o.color or self.shadow.color
   else
      self.shadow.shift = 1
   end
end

function Label:getText()
    return self.text
end

function Label:setText(o)
   if (type(o) == 'string') then
      self.text = o
   end
end

function Label:getTextSize()
   canvas:attrFont(self.activated.type, self.activated.size, self.activated.style)
   local nX, nY = canvas:measureText(self.text)
   return nX, nY
end

function Label:redraw()
   self.parent:update(self)
end

return Label
